import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'gt_common_ui_servier.dart';

class NativeActionSheetAction {
  final String text;
  final VoidCallback? onPressed;

  NativeActionSheetAction({required this.text, this.onPressed});
}

class NativeActionSheet {
  final String? title;

  final String? message;

  final int? selectedIndex;

  final List<NativeActionSheetAction> actions;

  /// ActionSheet
  /// @param title 标题
  /// @param message 信息
  /// @param actions 按钮列表
  /// @param selectedIndex 初始选中的按钮索引
  NativeActionSheet({this.title, this.message, required this.actions, this.selectedIndex});

  Future<void> show({VoidCallback? onCancel}) async {
    var actionTitles = actions.map((element) => element.text).toList();
    var result = await GTNativeUiServer.instance.hostApi.showActionSheet(title, message, actionTitles, selectedIndex);
    if (result == -1) {
      onCancel?.call();
    } else {
      var action = actions[result];
      action.onPressed!();
    }
  }
}
